<template>
  <sl-table :columns="columns" :data-source="data" row-key="productId">
    <template #bodyCell="{ record, column, index }">
      <div v-if="['price', 'adjustmentQuantity'].includes(column.dataIndex as string)">
        <a-input-number
          v-model:value="record[column.dataIndex as string]"
          :min="-9999"
          @change="numberChange(record)"
        ></a-input-number>
      </div>
      <div v-else-if="column.dataIndex === 'productBatch'">
        <a @click="showBatch(record)">{{ record.productBatch || "请点击选择" }}</a>
      </div>
      <div v-else-if="column.key === 'oper'">
        <a class="text-color-danger" @click="$emit('remove', index)">删除</a>
      </div>
      <div v-else-if="column.dataIndex === 'reason'">
        <a-input v-model:value="record[column.dataIndex as string]" style="width: 100px"></a-input>
      </div>
      <WidthFitCell
        v-else
        :title="column.title"
        :value="record[column.dataIndex as string]"
        :custom-render="column.customRender"
      />
    </template>
    <template v-if="summaryData" #summary>
      <a-table-summary fixed>
        <a-table-summary-row>
          <a-table-summary-cell>总计</a-table-summary-cell>
          <a-table-summary-cell :col-span="7"> </a-table-summary-cell>
          <a-table-summary-cell> {{ intlNumberFormat(summaryData.price) }} </a-table-summary-cell>
          <a-table-summary-cell> {{ intlNumberFormat(summaryData.adjustmentQuantity) }} </a-table-summary-cell>
          <a-table-summary-cell> {{ intlNumberFormat(summaryData.amount) }} </a-table-summary-cell>
          <a-table-summary-cell :col-span="2"> </a-table-summary-cell>
        </a-table-summary-row>
      </a-table-summary>
    </template>
    <a-modal
      v-model:visible="batchVisible"
      title="选择商品批次"
      :width="800"
      :body-style="{ padding: 0 }"
      @ok="handleBactchSel"
    >
      <TableSelect
        ref="batchSelIns"
        v-model:value="selBatch"
        :columns="batchColumns"
        :get-data-url="Api.goodsBatch"
        :field-names="{ value: 'productBatch', label: 'productBatch' }"
        row-key="productBatch"
        single-select
        :extra-params="batchParams"
      ></TableSelect>
    </a-modal>
  </sl-table>
</template>
<script setup lang="ts">
import dayjs, { Dayjs } from "dayjs";
import { ProductFileItem, ReceiptItem } from "/@/api/main/pis/models";
import { ref } from "vue";
import { Api } from "/@/api/main/pis";
import { SelectExpose } from "/@/types/components";
import { message } from "ant-design-vue";
import { ReceiptTableExpose } from "./useReceiptManage";
import { cloneDeep } from "lodash-es";
import { getFloatInteger, intlNumberFormat } from "/@/utils/common";

const props = defineProps<{
  data: Partial<ProductFileItem>[];
  summaryData?: Partial<ProductFileItem>;
  model: Partial<ReceiptItem>;
}>();

const emits = defineEmits(["remove"]);

const columns = ref([
  {
    title: "商品编码",
    dataIndex: "productCode",
  },
  {
    title: "商品条码",
    dataIndex: "barCode",
  },
  {
    title: "商品名称",
    dataIndex: "name",
  },
  {
    title: "计量单位",
    dataIndex: "measurementUnit",
  },
  {
    title: "规格",
    dataIndex: "specifications",
  },
  {
    title: "生产日期",
    dataIndex: "productDate",
    customRender({ text }) {
      return text ? dayjs(text).format("YYYY-MM-DD") : "--";
    },
  },
  {
    title: "截止保质期",
    dataIndex: "shelfLife",
    customRender({ text }) {
      return text ? dayjs(text).format("YYYY-MM-DD") : "--";
    },
  },
  {
    title: "商品批次",
    dataIndex: "productBatch",
  },
  {
    title: "进价",
    dataIndex: "price",
  },
  {
    title: "调拨数量",
    dataIndex: "adjustmentQuantity",
  },
  {
    title: "调拨金额",
    dataIndex: "amount",
  },
  {
    title: "原因",
    dataIndex: "reason",
  },
  {
    title: "操作",
    key: "oper",
    width: 100,
  },
]);
function numberChange(record: ProductFileItem) {
  let amount = 0;
  if (record.adjustmentQuantity && record.price) {
    amount = getFloatInteger(record.adjustmentQuantity * record.price);
  }
  record.amount = amount;
}

/**
 * 选择商品批次
 */
const batchVisible = ref(false);
const batchSelIns = ref<SelectExpose>();
const selBatch = ref<string[]>([]);
const batchColumns = ref([
  {
    title: "商品批次",
    dataIndex: "productBatch",
  },
  {
    title: "商品编码",
    dataIndex: "productCode",
  },
  {
    title: "商品条码",
    dataIndex: "barCode",
  },
  {
    title: "商品名称",
    dataIndex: "name",
  },
  {
    title: "计量单位",
    dataIndex: "measurementUnit",
  },
  {
    title: "规格",
    dataIndex: "specifications",
  },
  {
    title: "入库数量",
    dataIndex: "warehousingQuantity",
  },
  {
    title: "生产日期",
    dataIndex: "productDate",
    customRender({ text }) {
      return text ? dayjs(text).format("YYYY-MM-DD") : "--";
    },
  },
  {
    title: "截止保质期",
    dataIndex: "shelfLife",
    customRender({ text }) {
      return text ? dayjs(text).format("YYYY-MM-DD") : "--";
    },
  },
  {
    title: "进货价",
    dataIndex: "buyingPrice",
  },
  {
    title: "入库金额",
    dataIndex: "warehousingAmount",
  },
]);
const batchParams = ref<any>({});
function handleBactchSel() {
  if (selBatch.value.length === 0) {
    return message.info("请选择商品批次");
  }
  if (batchSelIns.value) {
    const items = batchSelIns.value.getSelecedItems() as ProductFileItem[];
    if (curRecordForBatch.value) {
      curRecordForBatch.value.productBatch = items[0].productBatch;
      curRecordForBatch.value.productDate = items[0].productDate;
      curRecordForBatch.value.shelfLife = items[0].shelfLife;
      batchVisible.value = false;
    }
  }
}
const curRecordForBatch = ref<ProductFileItem>();
function showBatch(record: ProductFileItem) {
  if (!props.model.deliveryWarehouseId) {
    return message.info("请先选择出货仓库");
  }
  curRecordForBatch.value = record;
  batchParams.value = {
    productId: record.productId,
    warehouseId: props.model.deliveryWarehouseId,
  };
  batchVisible.value = true;
}
/**
 *
 */
defineExpose<ReceiptTableExpose>({
  getProductData() {
    const details = cloneDeep(props.data);
    for (let i = 0; i < details.length; i++) {
      const item = details[i];
      if (!item.productBatch) {
        message.info("请选择各商品的商品批次");
        return null;
      }
      if (!item.adjustmentQuantity) {
        message.info("请输入各商品的调拨数量");
        return null;
      }
      if (!item.price) {
        message.info("请输入各商品的进价");
        return null;
      }
    }
    return details;
  },
});
</script>
